import { Injectable } from '@angular/core';
import { AlertController, Platform, LoadingController, ToastController } from 'ionic-angular';

declare var Wechat;
declare var QQSDK;
declare var WeiboSDK;
@Injectable()
export class ShareProvider {

  constructor(
    public AlertCtrl: AlertController,
    public platform: Platform,
    public loadingController: LoadingController,
    public ToastCtrl: ToastController
  ) {
  }
  wxShareWebpage(scene, link, title, description, thumbImg) {
    if (this.platform) {
      Wechat.isInstalled((installed) => {
        if (!installed) {
          this.alertMess("警告", "请安装微信！");
          return;
        }
      }, (reason) => {
        this.alertMess("Failed", reason);
      });
      var lodaing = this.loadingController.create({
        showBackdrop: false
      })
      lodaing.present();
      try {
        Wechat.share({
          message: {
            title: title,
            description: description,
            thumb: thumbImg == null ? "https://hmyd-images.b0.upaiyun.com/web/v2/logo.png" : thumbImg,
            mediaTagName: "TEST-TAG-001",
            messageExt: "这是第三方带的测试字段",
            messageAction: "<action>dotalist</action>",
            media: {
              type: Wechat.Type.WEBPAGE,
              webpageUrl: link
            }
          },
          scene: scene === 0 ? Wechat.Scene.SESSION : Wechat.Scene.Timeline
        }, () => {
          //成功
          this.toast("分享成功")
        }, () => {
          //失败
          this.toast("分享失败")
        })
      } catch (error) {

      } finally {
        lodaing.dismiss();
      }
    }
  }
  qqShareWebpage(scene, link, title, description, thumbImg) {
    var loading = this.loadingController.create({ showBackdrop: false });
    loading.present();
    try {
      var args: any = {};
      if (scene == 0) {
        args.scene = QQSDK.Scene.QQ;//QQSDK.Scene.QQZone,QQSDK.Scene.Favorite
      } else {
        args.scene = QQSDK.Scene.QQZone;
      }
      args.url = link;
      args.title = title;
      args.description = description;
      args.image = thumbImg == null ? "https://hmyd-images.b0.upaiyun.com/web/v2/logo.png" : thumbImg;
      console.log("QQSDK.shareNews", args);

      QQSDK.shareNews(function () {
        loading.dismiss();
      }, function (failReason) {
        loading.dismiss();
        alert("QQSDK.shareNews Failed: " + failReason);
      }, args);

    } catch (error) {
      console.log("QQSDK.share", error);
    } finally {
      loading.dismiss();
    }
  }
  wbShareWebpage(link, title, description, thumbImg) {
    var loading = this.loadingController.create({ showBackdrop: false });
    loading.present();
    try {
      var args: any = {};
      args.url = link;
      args.title = title;
      args.description = description;
      args.imageUrl = thumbImg == null ? "https://hmyd-images.b0.upaiyun.com/web/v2/logo.png" : thumbImg;
      args.defaultText = "";
      WeiboSDK.shareToWeibo(function () {

      }, function (failReason) {
        alert("weibo.share Failed:" + failReason);
      }, args);

    } catch (error) {
      console.log("wbShareWebpage.share  Failed:", error);
    } finally {
      loading.dismiss();
    }
  }

  alertMess(title, message) {
    let alert = this.AlertCtrl.create({
      title: title,
      message: message,
      buttons: ['确定']
    })
    alert.present();
  }
  toast(message) {
    let toast = this.ToastCtrl.create({
      message: message,
      duration: 1000,
      position: 'top'
    })
    toast.present();
  }
}
